<?php

    class Application_Model_Message extends Zend_Db_Table_Abstract
    {

        protected $_name = "messages";

        function init()
        {
            
        }

        //function to get messages by use of user id
        function getUserMessages($uid, $inbox = true, $paginator = false, $limit = NULL, $order = "message_id desc")
        {

            if (empty($uid))
                return false;

            if ($inbox)
                $where = "message_to = " . $uid;
            else
                $where = "message_from = " . $uid;

            $result = $this->select()->from("messages")
                    ->setIntegrityCheck(false);

            $subQuery = new Zend_Db_Expr("(select user_id, user_first_name, user_last_name, user_username, user_email from users where user_active='1'
                        union
                        select 0, 'Admin','Admin',admin_username,admin_username from admin)
                        ");

            if ($inbox)
                $result = $result->joinLeft(array("s" => $subQuery), "s.user_id = messages.message_from")->where("message_deleted_to = '0'");
            else
                $result = $result->joinLeft(array("s" => $subQuery), "s.user_id = messages.message_to")->where("message_deleted_from = '0'");


            //prd($result->__toString());

            $result = $result->where($where)
                    ->order($order);

            if ($limit)
                $result = $result->limit($limit);

            if ($paginator)
                return $result;

            $result = $result->query()->fetchAll();
            
            return $result;
        }

        function sendMessage($receiverId, $senderId = false, $msgContent)
        {

            if (!$senderId)
            {
                $x = new Zend_Auth_Storage_Session('User_Auth');
                $senderId = $x->read()->user_id;
            }

            $data = array();
            $data['message_to'] = $receiverId;
            $data['message_from'] = $senderId;
            $data['message_content'] = $msgContent;
            $result = $this->insert($data);

            return $result;
        }

        //function to get messages with the help of message id
        function getMessagebyId($id)
        {

            $dbSelect = $this->select()
                    ->from(array("m" => "messages"))
                    ->setIntegrityCheck(false)
                    ->join(array("u" => "users"), "u.user_id = m.message_from", array("sender_name" => 'concat(u.user_first_name," ",u.user_last_name)'))
                    ->join(array("u1" => "users"), "u1.user_id = m.message_to", array("receiver_name" => 'concat(u1.user_first_name," ",u1.user_last_name)'))
                    ->where("message_id = ?", $id);

            // echo $dbSelect->__toString(); die;
            $result = $dbSelect->query()->fetch();
            return $result;
        }

        function validateUserMessage($user_id, $message_id, $inbox = true)
        {

            if (!$user_id)
                return false;

            if ($inbox)
                $dbSelect = $this->select()->where("message_to = '" . $user_id . "' and message_id = '" . $message_id . "' ");
            else
                $dbSelect = $this->select()->where("message_from = '" . $user_id . "' and message_id = '" . $message_id . "' ");

            $result = $dbSelect->query()->fetch();

            return !empty($result) ? true : false;
        }

        function deleteMsg($message_id, $inbox = true)
        {

            if ($message_id)
            {
                if ($inbox)
                    $data['message_deleted_to'] = '1';
                else
                    $data['message_deleted_from'] = '1';

                $result = $this->update($data, 'message_id = ' . $message_id);

                $result = $this->select()->from("messages", array('message_deleted_from', 'message_deleted_to'))->where("message_id = ?", $message_id);
            }
        }

    }

?>
